package cn.signalpha.modules.com.controller.admin;

import cn.signalpha.common.constant.AppConstant;
import cn.signalpha.common.constant.MsgConstant;
import cn.signalpha.common.utils.Assert;
import cn.signalpha.core.boot.ctrl.BaseController;
import cn.signalpha.core.log.exception.ServiceException;
import cn.signalpha.core.mp.support.Condition;
import cn.signalpha.core.mp.support.Query;
import cn.signalpha.core.tool.api.R;
import cn.signalpha.core.tool.support.Kv;
import cn.signalpha.core.tool.utils.BeanUtil;
import cn.signalpha.core.tool.utils.Func;
import cn.signalpha.core.tool.utils.ObjectUtil;
import cn.signalpha.modules.com.dto.UserLevelDTO;
import cn.signalpha.modules.com.entity.UserLevel;
import cn.signalpha.modules.com.service.UserLevelService;
import cn.signalpha.modules.com.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 控制器
 *
 * @author signalpha
 */
@RestController
@AllArgsConstructor
@RequestMapping(AppConstant.APPLICATION_USER_NAME + "/admin/user/level")
@Api(value = "用户等级", tags = "用户等级")
public class UserLevelController extends BaseController {

	private final UserService userService;
	private final UserLevelService userLevelService;

	@GetMapping("/page")
	@ApiOperation(value = "分页", notes = "")
	public R<IPage<UserLevel>> page(@ApiIgnore @RequestParam Map<String, Object> params, Query query) {
		QueryWrapper<UserLevel> queryWrapper = userLevelService.selectUserLevelWrapper(params);
		return R.data(userLevelService.page(Condition.getPage(query), queryWrapper));
	}

	@GetMapping("/list")
	@ApiOperation(value = "列表", notes = "")
	public R<List<UserLevel>> list(@ApiIgnore @RequestParam Map<String, Object> params) {
		return R.data(userLevelService.list(userLevelService.selectUserLevelWrapper(params)));
	}

	@GetMapping("/info")
	@ApiOperation(value = "详情", notes = "传入id")
	public R<UserLevel> info(@ApiParam(value = "主键", required = true) @RequestParam Long id) {
		return R.data(userLevelService.getById(id));
	}

	@PostMapping("/save")
	@ApiOperation(value = "新增", notes = "传入userLevel")
	public R save(@Valid @RequestBody UserLevelDTO userLevelDTO) {
		if(ObjectUtil.isNotEmpty(userLevelService.getById(userLevelDTO.getId()))){
			throw new ServiceException("等级级别重复");
		}
		if(ObjectUtil.isNotEmpty(userLevelService.getByName(userLevelDTO.getName()))){
			throw new ServiceException("等级名称重复");
		}
		userLevelService.removeCaChe();
		return R.status(userLevelService.save(BeanUtil.copy(userLevelDTO, UserLevel.class)));
	}

	@PostMapping("/update")
	@ApiOperation(value = "修改", notes = "传入userLevel")
	public R update(@Valid @RequestBody UserLevelDTO userLevelDTO) {
		UserLevel userLevel = userLevelService.getById(userLevelDTO.getId());
		Assert.isNull(userLevel, MsgConstant.OBJECT_NULL);
		if(!Objects.equals(userLevelDTO.getId(), userLevel.getId())){
			if(ObjectUtil.isNotEmpty(userLevelService.getById(userLevelDTO.getId()))){
				throw new ServiceException("等级级别重复");
			}
		}
		if(!Objects.equals(userLevelDTO.getName(), userLevel.getName())){
			if(ObjectUtil.isNotEmpty(userLevelService.getByName(userLevelDTO.getName()))){
				throw new ServiceException("等级名称重复");
			}
		}
		userLevelService.removeCaChe();
		return R.status(userLevelService.updateById(BeanUtil.copy(userLevelDTO, UserLevel.class)));
	}

	@PostMapping("/remove")
	@ApiOperation(value = "删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		Func.toIntList(ids).forEach(id -> {
			if(userService.countByParams(Kv.create().set("level", id)) > 0){
				throw new ServiceException("该等级已关联用户，请先解除绑定关系");
			}
		});
		userLevelService.removeCaChe();
		return R.status(userLevelService.removeByIds(Func.toLongList(ids)));
	}

}
